//
//  SAInventoryCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/3.
//

#import "SAInventoryCell.h"
#import "SAInventoryListModel.h"
#import "SASalesOrderViewListModel.h"

@interface SAInventoryCell ()

@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *shopIdLab;
@property (nonatomic, strong) UILabel *shopTypeLab;
@property (nonatomic, strong) UILabel *stockCountLab;
@property (nonatomic, strong) UILabel *retailPriceLab;
@property (nonatomic, strong) UILabel *stockCostLab;


@end

@implementation SAInventoryCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAInventoryListRecods *)model {
    _model = model;

    NSString *newUrl = [[model.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    
    self.shopIdLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    self.shopTypeLab.text = [NSString stringWithFormat:@"入库价:￥%0.2f",[model.entryPrice doubleValue]/100];
    self.retailPriceLab.text = [NSString stringWithFormat:@"零售价:￥%0.2f",[model.retailPrice doubleValue]/100];
    
    self.stockCountLab.text = [NSString stringWithFormat:@"库存:%@",model.stockCount];
    self.stockCostLab.text = [NSString stringWithFormat:@"库存成本:￥%0.2f",[model.stockCost doubleValue]/100];
    
    self.icon.hidden = (model.earlyWarning != YES);
    
    NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
    if ([productViewCostPrice isEqualToString:@"1"]) {
        self.shopTypeLab.text = [NSString stringWithFormat:@"入库价:￥******"];
        self.stockCostLab.text = [NSString stringWithFormat:@"库存成本:￥******"];
    }
}

-(void)setSalesmodel:(SASalesOrderViewListModel *)salesmodel {
    _salesmodel = salesmodel;
    
    NSString *newUrl = [[salesmodel.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    self.shopIdLab.text = @"";
    self.shopTypeLab.text = @"";
    self.stockCountLab.text = salesmodel.productCode;
    self.retailPriceLab.text = salesmodel.productName;
    self.stockCostLab.text = @"";
    self.icon.hidden = YES;
    self.retailPriceLab.textColor = kGrayColor;
}

-(void)setIsRedBg:(BOOL)isRedBg {
    _isRedBg = isRedBg;
    if (isRedBg == YES) {
        self.backgroundColor = kColor(255, 228, 231, 1);
    }else {
        self.backgroundColor = [UIColor whiteColor];
    }
}

#pragma mark - initUI
-(void)initUI {

    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.shopIdLab];
    [self.contentView addSubview:self.shopTypeLab];
    [self.contentView addSubview:self.stockCountLab];
    [self.contentView addSubview:self.stockCostLab];
    [self.contentView addSubview:self.retailPriceLab];
    [self.contentView addSubview:self.icon];
    [self.contentView addSubview:self.line];
    
//    [self.stockLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(kScaleNum(25));
//        make.height.mas_equalTo(kScaleNum(20));
//        make.right.mas_equalTo(kScaleNum(-25));
//        make.width.mas_equalTo(kScaleNum(180));
//    }];
}

-(void)clickedImageV {
    !self.showImageBlock ?: self.showImageBlock();
}

#pragma mark - lazy
-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(26, 6, 53, 59)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageV)];
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
        _imageV.clipsToBounds = YES;
        [_imageV setContentScaleFactor:[[UIScreen mainScreen] scale]];
        _imageV.userInteractionEnabled = YES;
        [_imageV addGestureRecognizer:tap];
    }
    return _imageV;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 70, 375, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UILabel *)shopIdLab {
    if (!_shopIdLab) {
        _shopIdLab = [[UILabel alloc] initWithFrame:SARectMake(85, 5, 150, 20)];
        _shopIdLab.textColor = [UIColor blackColor];
        _shopIdLab.font = kFont(13);
    }
    return _shopIdLab;
}

-(UILabel *)shopTypeLab {
    if (!_shopTypeLab) {
        _shopTypeLab = [[UILabel alloc] initWithFrame:SARectMake(85, 25, 150, 20)];
        _shopTypeLab.textColor = kLightgGrayColor;
        _shopTypeLab.font = kFont(13);
    }
    return _shopTypeLab;
}

-(UILabel *)stockCountLab {
    if (!_stockCountLab) {
        _stockCountLab = [[UILabel alloc] initWithFrame:SARectMake(250, 25, 75, 20)];
        _stockCountLab.textAlignment = NSTextAlignmentLeft;
        _stockCountLab.textColor = kLightgGrayColor;
        _stockCountLab.font = kFont(13);
    }
    return _stockCountLab;
}

-(UILabel *)retailPriceLab {
    if (!_retailPriceLab) {
        _retailPriceLab = [[UILabel alloc] initWithFrame:SARectMake(85, 45, 150, 20)];
        _retailPriceLab.textColor = kLightgGrayColor;
        _retailPriceLab.textAlignment = NSTextAlignmentLeft;
        _retailPriceLab.font = kFont(13);
    }
    return _retailPriceLab;
}

-(UILabel *)stockCostLab {
    if (!_stockCostLab) {
        _stockCostLab = [[UILabel alloc] initWithFrame:SARectMake(250, 45, 125, 20)];
        _stockCostLab.textColor = kLightgGrayColor;
        _stockCostLab.textAlignment = NSTextAlignmentLeft;
        _stockCostLab.font = kFont(13);
    }
    return _stockCostLab;
}

-(UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] initWithFrame:SARectMake(kBaseScreenWidth - 50, 25, 21, 21)];
        _icon.image = [UIImage imageNamed:@"Inventory_warn"];
    }
    return _icon;
}

@end
